/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.demo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <p>Project: app2022 - ListStringSortDemo
 * <p>Powered by webrx On 2022-01-25 17:46:23
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class ListStringSortDemo {
    public static void main(String[] args) {
        var list = new ArrayList<String>(List.of("java", "c", "vb", "javascript", "c++", "ruby", "php", "python"));
        System.out.println(list);
        //根据字符串升序
        Collections.sort(list, (a, b) -> a.length() - b.length());
        Collections.sort(list, Comparator.comparingInt(String::length));
        System.out.println(list);

        //根据字符串降序
        Collections.sort(list, (a, b) -> b.length() - a.length());
        System.out.println(list);

        //根据字符串乱序
        Collections.shuffle(list);
        Collections.sort(list, (a, b) -> Math.random() > .5 ? -1 : 1);
        System.out.println(list);
    }

    public static void sort(String[] args) {
        //var list = new ArrayList<String>();
        var list = new ArrayList<String>(List.of("java", "c", "vb", "javascript", "c++", "ruby", "php", "python"));
        System.out.println(list);

        //升序
        Collections.sort(list);
        Collections.sort(list, (a, b) -> a.compareTo(b));
        Collections.sort(list, String::compareTo);
        System.out.println(list);


        //降序
        Collections.sort(list, (a, b) -> b.compareTo(a));
        Collections.sort(list, Comparator.reverseOrder());
        System.out.println(list);

        //乱序
        Collections.shuffle(list);
        System.out.println(list);

    }
}
